package cn.tlb.ui.widget;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListDemoActivity extends ListActivity {
    private ListView listView;
    private final int offset = 5;
    private int anchorPosition = 0;
    private static final String TAG = ListDemoActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView = getListView();
        final MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Log.i(TAG, String.format("onScroll(firstVisibleItem=%d, visibleItemCount=%d, totalItemCount=%d)", firstVisibleItem, visibleItemCount, totalItemCount));
                
                anchorPosition = firstVisibleItem + offset;
                adapter.notifyDataSetChanged();
            }
        });
    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 50;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = new TextView(ListDemoActivity.this);
            }
            TextView textView = (TextView) convertView;
            textView.setText("item "+position);
            if(position == anchorPosition){
                textView.setTextSize(30);
            }else{
                textView.setTextSize(20);
            }
            return convertView;
        }
    }
}
